package com.zizo.autohome.kb.dao;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.zizo.autohome.kb.bean.AutoHomeKB;
import com.zizo.core.DAO;
import com.zizo.core.T;
import com.zizo.util.Util;

public class KBDaoOfAutoHome extends DAO {

	/**
	 * @Title:KBDaoOfAutoHome
	 * @Description:
	 * @param currConn
	 * @param coreConn
	 */
	public KBDaoOfAutoHome(Connection currConn, Connection coreConn) {
		super(currConn, coreConn);
	}

	/**
	 * @Title: getSyn
	 * @Description: 查找carvoice_autohome中topic_syn表中的数据对应，返回Map表，用于同义词的替换
	 * @return Map<String,String>
	 * @author HeYan
	 */
	public Map<String, String> getSyn() {
		Map<String, String> map = new HashMap<>();
		Statement st = null;
		ResultSet rs = null;
		String sql = "select syn_word,syn_syn from carvoice_autohome.topic_syn";
		try {
			st = currConn.createStatement();
			rs = st.executeQuery(sql);
			while (rs.next()) {
				map.put(rs.getString("syn_word"), rs.getString("syn_syn"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return map;
	}

	/**
	 * @Title: insertGCYT
	 * @Description: 插入口碑的购车用途
	 * @param carid
	 * @param kb_kbid
	 * @param kb_gcmd
	 * @return: void
	 * @author: Alexis
	 */
	public void insertGCYT(int kb_ppid, int kb_kbid, int kb_gcyt) {
		PreparedStatement ps = null;
		String sql = "REPLACE INTO autohome_other.other_kb_aim(aim_cid,aim_kbid,aim_yt,aim_uptime) VALUES(?,?,?,?)";
		try {
			ps = currConn.prepareStatement(sql);
			ps.setInt(1, kb_ppid);
			ps.setInt(2, kb_kbid);
			ps.setInt(3, kb_gcyt);
			ps.setTimestamp(4, (Timestamp) Util.getTimeHMS(Util.getCurrentDateTime()));
			ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	/*
	 * @Title: insertNUM
	 * 
	 * @Description: 插入口碑的浏览数以及回复数
	 * 
	 * @return: void
	 * 
	 * @author: li
	 */
	public void insertNUM(int kb_kbid, int kb_ppid, int kb_cxid, String kb_plrq, int kb_lls, int kb_pls) {
		PreparedStatement ps = null;
		String sql = "REPLACE INTO autohome_other.other_kb_count(count_kbid,count_cid,count_mid,count_ptime,count_kb_view,count_kb_discuss,count_uptime) VALUES(?,?,?,?,?,?,?)";
		try {
			ps = currConn.prepareStatement(sql);
			ps.setInt(1, kb_kbid);
			ps.setInt(2, kb_ppid);
			ps.setInt(3, kb_cxid);
			ps.setDate(4, (Date) Util.getDate(kb_plrq));
			ps.setInt(5, kb_lls);
			ps.setInt(6, kb_pls);
			ps.setTimestamp(7, (Timestamp) Util.getTimeHMS(Util.getCurrentDateTime()));
			ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	/*
	 * (non Javadoc)
	 * 
	 * @Title: getIDs
	 * 
	 * @Description:
	 * 
	 * @return
	 * 
	 * @see com.zizo.core.DAOInterface#getIDs()
	 */
	@Override
	public List<String> getIDs() {
		List<String> carIDs = new ArrayList<String>();
		Statement st = null;
		ResultSet rs = null;
		String sql = "SELECT autohome_autohomeId FROM carvoice_core.core_brand_autohome WHERE autohome_id<=1000 and autohome_id>0;";
		try {
			st = currConn.createStatement();
			rs = st.executeQuery(sql);
			while (rs.next()) {
				carIDs.add(rs.getString("autohome_autohomeId"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return carIDs;
	}

	/*
	 * (non Javadoc)
	 * 
	 * @Title: insertData
	 * 
	 * @Description:
	 * 
	 * @param t
	 * 
	 * @return
	 * 
	 * @see com.zizo.core.DAOInterface#insertData(com.zizo.core.T)
	 */
	@Override
	public int insertData(T t) {
		AutoHomeKB kb = (AutoHomeKB) t;
		PreparedStatement ps = null;
		String sql = "REPLACE INTO autohome_other.other_kb(kb_kbid, kb_cid, kb_mid, kb_user_name, kb_uid, kb_ptime, kb_province, kb_area, kb_ssss, kb_kj, kb_dl, kb_ck, kb_yh, kb_ssx, kb_wg, kb_ns, kb_xjb, kb_content, kb_length, kb_btime, kb_purchase_price, kb_syyh,kb_lc, kb_support, kb_view, kb_discuss, kb_uptime,kb_aim) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
		try {
			ps = currConn.prepareStatement(sql);
			ps.setInt(1, kb.getKb_kbid());
			ps.setInt(2, kb.getKb_ppid());
			ps.setInt(3, kb.getKb_cxid());
			ps.setString(4, kb.getKb_yhmc());
			ps.setInt(5, kb.getKb_yhid());
			ps.setDate(6, (Date) Util.getDate(kb.getKb_plrq()));
			ps.setString(7, kb.getKb_gmdd());
			ps.setString(8, kb.getKb_gmqy());
			ps.setString(9, kb.getKb_jxs());
			ps.setFloat(10, kb.getKb_kj());
			ps.setFloat(11, kb.getKb_dl());
			ps.setFloat(12, kb.getKb_ck());
			ps.setFloat(13, kb.getKb_yh());
			ps.setFloat(14, kb.getKb_ssx());
			ps.setFloat(15, kb.getKb_wg());
			ps.setFloat(16, kb.getKb_ns());
			ps.setFloat(17, kb.getKb_xjb());
			ps.setString(18, kb.getKb_plnr());
			ps.setInt(19, kb.getKb_plcd());
			ps.setDate(20, (Date) Util.getDate(kb.getKb_gmsj()));
			ps.setFloat(21, kb.getKb_gmjg());
			ps.setFloat(22, kb.getKb_syyh());
			ps.setInt(23, kb.getKb_lc());
			ps.setInt(24, kb.getKb_zcs());
			ps.setInt(25, kb.getKb_lls());
			ps.setInt(26, kb.getKb_pls());
			ps.setTimestamp(27, (Timestamp) Util.getTimeHMS(Util.getCurrentDateTime()));
			ps.setString(28, kb.getKb_gcmd());
			ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return 0;
	}

	/**
	 * @Title: insertSynData
	 * @Description: TODO 替换同义词
	 * @param t
	 * @return
	 * @return int
	 * @author Alexis 
	 */
	public int insertSynData(T t) {
		AutoHomeKB kb = (AutoHomeKB) t;
		PreparedStatement ps = null;
		String sql = "REPLACE INTO autohome_syn.autohome_kb(kb_kbid, kb_cid, kb_mid, kb_user_name, kb_uid, kb_ptime, kb_province, kb_area, kb_ssss, kb_kj, kb_dl, kb_ck, kb_yh, kb_ssx, kb_wg, kb_ns, kb_xjb, kb_content, kb_length, kb_btime, kb_purchase_price, kb_syyh,kb_lc, kb_support, kb_view, kb_discuss, kb_uptime,kb_aim) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
		try {
			ps = currConn.prepareStatement(sql);
			ps.setInt(1, kb.getKb_kbid());
			ps.setInt(2, kb.getKb_ppid());
			ps.setInt(3, kb.getKb_cxid());
			ps.setString(4, kb.getKb_yhmc());
			ps.setInt(5, kb.getKb_yhid());
			ps.setDate(6, (Date) Util.getDate(kb.getKb_plrq()));
			ps.setString(7, kb.getKb_gmdd());
			ps.setString(8, kb.getKb_gmqy());
			ps.setString(9, kb.getKb_jxs());
			ps.setFloat(10, kb.getKb_kj());
			ps.setFloat(11, kb.getKb_dl());
			ps.setFloat(12, kb.getKb_ck());
			ps.setFloat(13, kb.getKb_yh());
			ps.setFloat(14, kb.getKb_ssx());
			ps.setFloat(15, kb.getKb_wg());
			ps.setFloat(16, kb.getKb_ns());
			ps.setFloat(17, kb.getKb_xjb());
			ps.setString(18, kb.getKb_plnr());
			ps.setInt(19, kb.getKb_plcd());
			ps.setDate(20, (Date) Util.getDate(kb.getKb_gmsj()));
			ps.setFloat(21, kb.getKb_gmjg());
			ps.setFloat(22, kb.getKb_syyh());
			ps.setInt(23, kb.getKb_lc());
			ps.setInt(24, kb.getKb_zcs());
			ps.setInt(25, kb.getKb_lls());
			ps.setInt(26, kb.getKb_pls());
			ps.setTimestamp(27, (Timestamp) Util.getTimeHMS(Util.getCurrentDateTime()));
			ps.setString(28, kb.getKb_gcmd());
			ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return 0;
	}

	/*
	 * (non Javadoc)
	 * 
	 * @Title: updateTime
	 * 
	 * @Description:
	 * 
	 * @param type
	 * 
	 * @param id
	 * 
	 * @see com.zizo.core.DAOInterface#updateTime(java.lang.String,
	 * java.lang.String)
	 */
	@Override
	public void updateTime(String type, String id) {

	}

	/*
	 * (non Javadoc)
	 * 
	 * @Title: getTables
	 * 
	 * @Description:
	 * 
	 * @return
	 * 
	 * @see com.zizo.core.DAOInterface#getTables()
	 */
	@Override
	public List<String> getTables() {
		return null;
	}

	/*
	 * (non Javadoc)
	 * 
	 * @Title: insertError
	 * 
	 * @Description:
	 * 
	 * @param t
	 * 
	 * @return
	 * 
	 * @see com.zizo.core.DAOInterface#insertError(com.zizo.core.T)
	 */
	@Override
	public int insertError(T t) {
		return 0;
	}

//	public static void main(String[] args) {
//		Connection currConn = ConnectionPool.getConnection("autohome", "carvoice.tpddns.cn");
//		Connection coreConn = ConnectionPool.getConnection("carvoice_core", "carvoice.tpddns.cn");
//		KBDaoOfAutoHome dao = new KBDaoOfAutoHome(currConn, coreConn);
//		for (String s : dao.getIDs()) {
//			System.out.println(s);
//		}
//	}
}
